package ru.urfc.portal.ui.client.googlets.admin;

import ru.urfc.portal.ui.client.BaseGooglet;
import ru.urfc.portal.ui.client.Style;
import ru.urfc.portal.ui.client.Utils;
import ru.urfc.portal.ui.client.controllers.ItemsController;
import ru.urfc.portal.ui.client.controllers.events.PutEvent;
import ru.urfc.portal.ui.client.descriptor.GridDescriptor;

import com.extjs.gxt.ui.client.data.ModelData;
import com.extjs.gxt.ui.client.event.BaseEvent;
import com.extjs.gxt.ui.client.event.Events;
import com.extjs.gxt.ui.client.event.Listener;
import com.extjs.gxt.ui.client.widget.form.CheckBox;
import com.extjs.gxt.ui.client.widget.form.ComboBox;
import com.google.gwt.core.client.GWT;
import com.google.gwt.dom.client.Element;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.shared.GwtEvent.Type;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.client.ui.Widget;

public class ClientNotifications extends BaseGooglet {

	interface Binder extends UiBinder<Widget, ClientNotifications> {}
	private static Binder binder = GWT.create(Binder.class);
	
	@UiField Style style;

	@UiField CheckBox globalDeviceCertificateExpiring;
	@UiField CheckBox globalNetLicenseExpiring;
	@UiField ComboBox<ModelData> globalExpirationNotifyInterval;
	@UiField CheckBox globalNotifyAboutUnpaidInvoices;
	@UiField ComboBox<ModelData> globalInvoiceUnpaidDays;
	@UiField ComboBox<ModelData> globalInvoiceUnpaidNotifyInterval;
	
	@UiField CheckBox useGlobal;
	@UiField CheckBox notifyAboutUnpaidInvoices;
	@UiField ComboBox<ModelData> invoiceUnpaidDays;
	@UiField ComboBox<ModelData> invoiceUnpaidNotifyInterval;
	
	@UiField Element clientSettings;
	@UiField Element globalSettings;
	
	public Type<ItemsController> SAVE = new Type<ItemsController>();

	@UiHandler("save")
	void changeAccess(ClickEvent e) {
		getEventsDispatcher().fireEvent(new PutEvent(SAVE));
	}
	
	@Override
	protected void init() {
		initWidget(binder.createAndBindUi(this));
		changeSettings();
		useGlobal.addListener(Events.Change, new Listener<BaseEvent>() {
			@Override
			public void handleEvent(BaseEvent event) {
				changeSettings();
			}
		});
		
		GlobalNotifications.initComboBox(globalExpirationNotifyInterval);
		GlobalNotifications.initComboBox(globalInvoiceUnpaidDays);
		GlobalNotifications.initComboBox(globalInvoiceUnpaidNotifyInterval);
		GlobalNotifications.initComboBox(invoiceUnpaidDays);
		GlobalNotifications.initComboBox(invoiceUnpaidNotifyInterval);

		GridDescriptor descriptor = new GridDescriptor(NOTIFICATION_SETTINGS,
				CLIENTS, getItemId(), CLIENT_NOTIFICATION_SETTINGS);
		descriptor.addBoolInverseField("useGlobal", useGlobal);
		descriptor.addBoolField("notifyAboutUnpaidInvoices", notifyAboutUnpaidInvoices);
		descriptor.addSelectField("invoiceUnpaidDays", invoiceUnpaidDays);
		descriptor.addSelectField("invoiceUnpaidNotifyInterval", invoiceUnpaidNotifyInterval);
		register(SAVE, (ItemsController)createController(descriptor));
		
		initGlobal();
	}

	private void changeSettings() {
		if (useGlobal.getValue()) {
			Utils.show(clientSettings);
			Utils.hide(globalSettings);
		} else {
			Utils.show(globalSettings);
			Utils.hide(clientSettings);
		}
	}
	
	private void initGlobal() {
		GridDescriptor descriptor = new GridDescriptor(NOTIFICATION_SETTINGS, GLOBAL_NOTIFICATION_SETTINGS);
		descriptor.addBoolField("deviceCertificateExpiring", globalDeviceCertificateExpiring);
		descriptor.addBoolField("netLicenseExpiring", globalNetLicenseExpiring);
		descriptor.addSelectField("expirationNotifyInterval", globalExpirationNotifyInterval);
		descriptor.addBoolField("notifyAboutUnpaidInvoices", globalNotifyAboutUnpaidInvoices);
		descriptor.addSelectField("invoiceUnpaidDays", globalInvoiceUnpaidDays);
		descriptor.addSelectField("invoiceUnpaidNotifyInterval", globalInvoiceUnpaidNotifyInterval);
		createController(descriptor);
	}

}